package com.example.mysocketdemo;


import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Handler;
import android.os.HandlerThread;
import android.util.Log;

import java.io.DataOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Timer;
import java.util.TimerTask;

/**
 * Created by wangxin on 2017/8/30.
 */

public class BootUpReceiver extends BroadcastReceiver implements ClientConnector.ConnectLinstener {
    private ClientConnector mConnector;
    //genymotion连本地网络是10.0.3.2，IDE自带的模拟器是10.0.0.2
    private String mDstName = "192.168.1.200";
    private int mDstPort = 5858;
    private HandlerThread mHandlerThread;
    private Handler mHandler;
    private Thread socketThread = new Thread(new Runnable() {
        @Override
        public void run() {
            try {
                mConnector.connect();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    });
    @Override
    public void onReceive(Context context, Intent intent) {
        if (intent.getAction().toString().equals(Intent.ACTION_BOOT_COMPLETED)) {
            Log.e("BootUpReceiver", "开机自启动");
            Intent i = new Intent(context, MainActivity.class);
            i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            context.startActivity(i);
            /*Intent intent1 = new Intent(context, Service1.class);
            intent1.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            context.startService(intent1);*/
            /*Intent i = new Intent(context, MainActivity.class);
            i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            context.startActivity(i);*/

//            mHandlerThread = new HandlerThread("MainActivity", android.os.Process.THREAD_PRIORITY_BACKGROUND);
//            mHandlerThread.start();
//            mHandler = new Handler(mHandlerThread.getLooper());
           /* mConnector = new ClientConnector(mDstName, mDstPort);
            mConnector.setOnConnectLinstener(this);
            socketThread.run();*/
//            mHandler.post(new ConnectRunnable());

            /*new Thread(new Runnable() {
                @Override
                public void run() {
                    try {

                        mConnector.connect();
                        mConnector.auth("A");
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }).run();*/

        }
    }

    private class ConnectRunnable implements Runnable {


        @Override
        public void run() {
            try {

                mConnector.connect();

                TimerTask timerTask = new TimerTask() {
                    @Override
                    public void run() {
                        try {
                            Log.e("BootUpReceiver", "认证设备");
                            mConnector.auth("A");
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                };
                Timer timer = new Timer();
                timer.schedule(timerTask, 500);

            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    @Override
    public void onReceiveData(String data) {
        if (data.contains("START")) {
            execShellCmd("input tap " + 360 + " " + 1200);
        } else if (data.contains("STOP")) {
            execShellCmd("input tap " + 360 + " " + 1200);
            TimerTask timerTask = new TimerTask() {
                @Override
                public void run() {
                    execShellCmd("input tap " + 400 + " " + 1200);
                }
            };
            Timer timer = new Timer();
            timer.schedule(timerTask,500);
        }
    }

    private void execShellCmd(String cmd) {
        try {
            // 申请获取root权限
            Process process = Runtime.getRuntime().exec("su");
            // 获取输出流
            OutputStream outputStream = process.getOutputStream();
            DataOutputStream dataOutputStream = new DataOutputStream(
                    outputStream);
            dataOutputStream.writeBytes(cmd);
            dataOutputStream.flush();
            dataOutputStream.close();
            outputStream.close();
        } catch (Throwable t) {
            t.printStackTrace();
        }
    }
}
